#include "ActionSprite.h"


ActionSprite::ActionSprite(void)
	:_strClass("")
	,_actionState( e_action_state_null )
	,_actionDirection(0)
	,_defautActionName("")
	,_defaultActionState( e_action_state_null )
	,_timeConut(0)
{
}


ActionSprite::~ActionSprite(void)
{
}

ActionSprite* ActionSprite::create( )
{
	ActionSprite *sprite = new ActionSprite();
	if (sprite && sprite->init())
	{
		return sprite;
	}
	CC_SAFE_DELETE(sprite);
	return nullptr;
}

void ActionSprite::setClass( string className )
{
	_strClass = className;
}


void ActionSprite::setPath( string path )
{
	_path = path;
}

void ActionSprite::addDefaultAction( EACTION_STATE state, string actionName, int actionCount )
{
	_defaultActionState = state;
	_defautActionName = actionName;
	_defaultActionCount = actionCount;
}

void ActionSprite::setActionDirection( int direction )
{
	_actionDirection = direction;

	setActionSprite();
}


void ActionSprite::setAction(	string actionName,  
													int actionCount /*= 0*/,
													EACTION_STATE state /*= e_action_state_stand*/, 
													EACTION_TYPE type /*= e_action_type_Infinite*/, 
													float times /*= 0 */ )
{
	_actionName = actionName;
	_actionCount = actionCount;
	_actionState = state;
	_actionType = type;
	_times = times;

	setActionSprite();
}


void ActionSprite::setActionSprite()
{
	Animation* animation = getAnimation( _actionName, _actionCount );
	switch (_actionType)
	{
	case e_action_type_one:
		setOneAction(animation);
		break;
	case e_action_type_Infinite:
		setInfinteAction(animation);
		break;
	case e_action_type_time:
		setTimeAction(animation);
		break;
	}
}

Animation* ActionSprite::getAnimation( string actionName, int actionCount/* = 0*/ )
{
	Animation* an = Animation::create();
	char buff[128] ={0};
	snprintf( buff,sizeof(buff), "%s%s%/img.plist", _path.c_str(), _strClass.c_str() );
	SpriteFrameCache::getInstance()->addSpriteFramesWithFile(buff);

	for( int i=0; i<actionCount; i++ )
	{
		snprintf( buff, sizeof(buff), "%s_%s_%d_%d.png", _strClass.c_str(), actionName.c_str(), _actionDirection, i );
		SpriteFrame* frame = SpriteFrameCache::getInstance()->getSpriteFrameByName(buff);
		an->addSpriteFrame(frame);
	}
	an->setDelayPerUnit(0.03f);

	return an;
}

void ActionSprite::setOneAction( Animation* action )
{
	CallFunc *callbackIdle = CallFunc::create(std::bind(&ActionSprite::callBack, this));
	Sequence *repeat = Sequence::create(CCAnimate::create(action), callbackIdle, NULL);
	this->runAction(repeat);
}

void ActionSprite::setInfinteAction( Animation* action )
{
	RepeatForever *repeat = RepeatForever::create(CCAnimate::create(action));
	this->runAction(repeat);
}

void ActionSprite::setTimeAction( Animation* action )
{
	RepeatForever *repeat = RepeatForever::create(CCAnimate::create(action));
	this->runAction(repeat);

	_timeConut = _times;
}

void ActionSprite::setDefaultAction()
{
	_actionName	= _defautActionName;
	_actionCount	= _defaultActionCount;
	_actionState		= _defaultActionState;
	_actionType		= e_action_type_Infinite;

	setActionSprite();
}

void ActionSprite::update( float delta )
{
	if (_actionType == e_action_type_time && _timeConut > 0)
	{
		_timeConut -= delta;
		if (_timeConut < 0)
		{
			_timeConut = 0;
			setDefaultAction();
		}
	}
}


void ActionSprite::callBack()
{
	setDefaultAction();
}
